﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Models;
using Rose.Helper;

namespace Rose.Controllers
{  
    
    public class RelationController : Controller
    {
        //
        // GET: /Relation/


        #region 朋友
        private Database_RoseEntities db = new Database_RoseEntities();
        private FriendRelationHelpModel friendrelatiohelpmodel = new FriendRelationHelpModel();
        [Authorize]
        [HttpPost]
        public ActionResult AddFriend(string friendId)
        {
            
            
            try
            {
                UserInfo friendInfo = new CheckHelpModel().CheckUserInfo(friendId);
                string selfId = new CheckHelpModel().CheckCurrentUserId();
                //two check before the request can be sent
                if (friendrelatiohelpmodel.IsRequestExist(selfId, friendInfo.Id))
                    return Content("请求已发出，请等待对方回复");
                if (friendrelatiohelpmodel.IsRelationExist(selfId, friendInfo.Id))
                    return Content("你跟"+friendInfo.Name+"已是好友");

                if (friendrelatiohelpmodel.RequestToFriend(selfId, friendId))
                    return Content("你的请求已发送给" + friendInfo.Name);

                return Content("错误");
            }
            catch (Exception e)
            {
                return Content("错误-" + e.Message);
            }

        }
        [Authorize]
        [HttpPost]
        public ActionResult DeleteFriend(string friendId)
        {
            try
            {
                string selfId = new CheckHelpModel().CheckCurrentUserId();
                UserInfo friendInfo = new CheckHelpModel().CheckUserInfo(friendId);
                if (friendrelatiohelpmodel.IsRelationExist(selfId, friendId))
                {
                    friendrelatiohelpmodel.DeleteSingleFriendRelation(selfId, friendId);
                    return Content("成功删除跟"+friendInfo.Name+"的好友关系");
                }
                return Content("成功删除跟" + friendInfo.Name + "的好友关系");
            }
            catch(Exception ex)
            {
                return Content("错误-" + ex.Message);
            }

        }

        [Authorize]
        [HttpPost]
        public ActionResult AcceptRequestFriend(string requestId)
        {
            RequestFriend requestFriend =new CheckHelpModel().CheckRequestFriend(requestId);
            
            //q:
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            UserInfo selfInfo =new  CheckHelpModel().CheckUserInfo(selfId);

            UserInfo friendInfo =new  CheckHelpModel().CheckUserInfo(requestFriend.UserId);
            try
            {
                friendrelatiohelpmodel.AcceptRequestAndCreateRelation(requestId, selfId, friendInfo.Id);
                return Content("你添加" + friendInfo.Name+"为好友");
            }
            catch(Exception ex)
            {
                return Content("错误-"+ex.Message);
                
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult RejectRequestFriend(string requestId)
        {
            try
            {
                RequestFriend requestFriend =new CheckHelpModel().CheckRequestFriend(requestId);
                string requestUserId = requestFriend.UserId;
                UserInfo requestUserInfo =new CheckHelpModel().CheckUserInfo(requestUserId);
                friendrelatiohelpmodel.RejectRequest(requestId);
                return Content("你拒绝了" + requestUserInfo.Name+"的好友请求");
            }
            catch (Exception ex)
            {
                return Content("错误-" + ex.Message);
            }
        }
        [Authorize]
        [HttpPost]
        public ActionResult ConfirmFeedBack(string requestId)
        {
            try
            {

                friendrelatiohelpmodel.DeleteRequestRelation(requestId);
                
                return Content("更多的人脉能给你带来更多的积分哦");
            }
            catch (Exception ex)
            {
                return Content("错误-" + ex.Message);
            }
        }
        [Authorize]
        public ActionResult ShowFriendList(int currentpage=1)
        {
            ViewData["PageTitle"] = "我的所有好友";
            if (currentpage == 0)
                currentpage = 1;
            const int PAGESIZE = 20;
            string selfId=new CheckHelpModel().CheckCurrentUserId();
            List<RecommendModel> friendList = friendrelatiohelpmodel.GetFriendAbstractList(selfId, currentpage, PAGESIZE);

            return View(friendList);
        }

        [Authorize]
        public ActionResult ShowRequestFromOthers()
        {
            ViewData["PageTitle"] = "Request List";
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            List<RequestModel> requestList = friendrelatiohelpmodel.GetRequestFromOthers(selfId);

             return View(requestList);
            
        }

        [Authorize]
        public ActionResult ShowUnSettledRequestPart()
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            List<RequestModel> requestList = friendrelatiohelpmodel.GetUnSettledRequestFeedBacks(selfId);
            return View(requestList);

        }
        
        [Authorize]
        
        public ActionResult ShowAcceptRequestPart()
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            List<RequestModel> requestList = friendrelatiohelpmodel.GetAcceptRequestFeedBacks(selfId);
            return View(requestList);
        }

        [Authorize]
        public ActionResult ShowRejectRequestPart()
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            List<RequestModel> requestList = friendrelatiohelpmodel.GetRejectRequestFeedBacks(selfId);
            return View(requestList);
        }

        [Authorize]
        public ActionResult ShowFeedBack()
        {
            ViewData["PageTitle"] = "所有回馈";
            return View();
        }
        #endregion


        #region 关注
        FocusRelationHelpModel focusrelationhelpmodel = new FocusRelationHelpModel();
        [Authorize]
        [HttpPost]
        public ActionResult AddFocus(string focusId)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            UserInfo focusInfo=new  CheckHelpModel().CheckUserInfo(focusId);
            if (focusrelationhelpmodel.IsRelationExist(selfId, focusId))
            { 
                 return Content("你已关注" + focusInfo.Name);
            }
            try
            {
                if (focusrelationhelpmodel.AddFocusToRelation(selfId, focusId))
                {
                    return Content("关注" + focusInfo.Name+"成功");
                }
                return Content("错误");
            }
            catch (Exception ex)
            {
                return Content("错误" + ex.Message);
            }
          
            
        }

        [Authorize]
        [HttpPost]
        public ActionResult DeleteFocus(string focusId)
        {
            try
            {
                string selfId = new CheckHelpModel().CheckCurrentUserId();
                UserInfo focusInfo =new  CheckHelpModel().CheckUserInfo(focusId);
                if (focusrelationhelpmodel.IsRelationExist(selfId, focusId))
                {
                    focusrelationhelpmodel.RemoveFoucsFromRelation(selfId, focusId);
                    return Content("取消对" + focusInfo.Name+"的关注");

                }
                return Content("取消对"+focusInfo.Name + "的关注");
                
            }
            catch(Exception ex)
            {
                return Content("错误-" + ex.Message);
            }
        }

        [Authorize]
        public ActionResult ShowFocusList(int currentpage=1)
        {
            ViewData["PageTitle"] = "All Focus Friend";
            if (currentpage == 0)
                currentpage = 1;
            const int PAGESIZE = 20;
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            List<RecommendModel> friendList = focusrelationhelpmodel.GetFriendAbstractList(selfId, currentpage, PAGESIZE);

            return View(friendList);

        }
        #endregion


        #region 恋人
        private LoverRelationHelpModel loverrelationhelpmodel = new LoverRelationHelpModel();
        [Authorize]
        [HttpPost]
        [OutputCache(Duration = 0, VaryByParam = null)]
        public ActionResult CreateRequestLover()
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if (loverrelationhelpmodel.IsSelfLoverExist(selfId))
                return Content("你已经有情侣了");

            string requestId = loverrelationhelpmodel.RequestToLover(selfId);
            if (string.IsNullOrEmpty(requestId))
                return Content("你的情侣邀请已经发出");

            return Content("/Relation/RegisterForLover?requestId=" + requestId + "&loverId=" + selfId);
        }

        [Authorize]
        public ActionResult RegisterForLover(string requestId,string loverId)
        { 
            
            ViewData["PageTitle"] = "注册情侣关系";
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if (loverrelationhelpmodel.IsRequestBeCleard(requestId))
            return Content("该链接已不存在，请返回首页");

            if (!loverrelationhelpmodel.IsGenderDifferent(loverId, selfId)) 
            return Content("本网站暂时未开通同性恋爱功能，请返回首页");

            RequestModel requestModel = loverrelationhelpmodel.GetRequestLover(requestId);
            
            return View(requestModel);
            //连接至该人的头像;
            
        }


              
        [Authorize]
        [HttpPost]
        public ActionResult AcceptRequestLover(string requestId,bool open)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if (loverrelationhelpmodel.IsRequestBeCleard(requestId))
                return Content("该链接已过时");
            if (loverrelationhelpmodel.IsSelfLoverExist(selfId))
                return Content("你不能重复注册情侣");
            RequestLover requestLover =new CheckHelpModel().CheckRequestLover(requestId);
            UserInfo loverInfo =new CheckHelpModel().CheckUserInfo(requestLover.UserId);
            if (loverrelationhelpmodel.AcceptRequestAndUpdateDatabase(requestId, selfId, open))
                return Content("你成功与"+loverInfo.Name+"注册为情侣");
            return Content("错误");

        }

        [Authorize]
        [HttpPost]
        public ActionResult RejectRequestLover(string requestId)
        {
            try
            {
                if (loverrelationhelpmodel.IsRequestBeCleard(requestId))
                    return Content("你已接受成为情侣，请到管理界面进行注销");
                string selfId = new CheckHelpModel().CheckCurrentUserId();
                loverrelationhelpmodel.RejectRequest(requestId, selfId);
                return Content("你拒绝了情侣邀请");
                
            }
            catch (Exception ex)
            { 
                return Content("错误-"+ex.Message);
            }
        }

        [Authorize]
        [HttpPost]
        
        public ActionResult CloseLoverRelation()
        {
            string selfId=new CheckHelpModel().CheckCurrentUserId();
            try
            {
                if (loverrelationhelpmodel.IsSelfLoverExist(selfId))
                {

                    loverrelationhelpmodel.DeleteLoverRelation(selfId);
                    return Content("你解除了解除情侣关系");
                }
                return Content("你还没有情侣");
            }
            catch
            {
                return Content("错误");
            }
        }
        #endregion


        #region 新的邀请好友的方式
       
        [Authorize]
        [HttpPost]
        [OutputCache(Duration = 0, VaryByParam = null)]
        public ActionResult CreateRequestFriend()
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            string requestId = friendrelatiohelpmodel.InviteFriend(selfId);
            if (string.IsNullOrEmpty(requestId))
                return Content("生成邀请码失败");
            return Content("/Relation/RegisterForFriend?requestId=" + requestId + "&invitorId=" + selfId);

        }


        public ActionResult RegisterForFriend(string requestId, string invitorId)
        {
            ViewData["PageTitle"] = "好友邀请注册";
            ViewData["Gender"] = new ListHelpModel().GetGenderList("M");
            if (!friendrelatiohelpmodel.IsRequestExist(requestId))
                return Content("该链接已不存在，请返回首页");

            RequestModel requestModel = friendrelatiohelpmodel.GetRequestFriend(requestId);

            return View(requestModel);

        }


        [HttpPost]
        public ActionResult AcceptFriendInvitation(string requestId)
        {
            ///注册成功后就可以删除了；

            string selfId = new CheckHelpModel().CheckCurrentUserId();

            if (friendrelatiohelpmodel.IsRequestExist(requestId))
            {
                RequestFriend requestFriend =new CheckHelpModel().CheckRequestFriend(requestId);
                UserInfo friendInfo =new CheckHelpModel().CheckUserInfo(requestFriend.UserId);
                if (friendrelatiohelpmodel.InviteFriendSuccess(selfId, requestFriend.UserId, requestId))
                    return Content("你接受了" + friendInfo.Name+"的注册邀请");

            }
            return Content("错误");


        }

        #endregion
    }
}
